package com.zhxh.nzpos.member;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CardDao;
import com.zhxh.nzpos.dao.MemberDao;
import com.zhxh.nzpos.model.Card;
import com.zhxh.nzpos.model.Member;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 会员管理--信息录入界面
 * 
 * @author wangzt
 *
 */
public class InfoInputActivity extends BaseActivity {

	private final static String LOG_TAG = "InfoInputActivity";
	
	MemberDao memberDao;
	CardDao  cardDao;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_info_input);
        memberDao = new MemberDao(this);
        cardDao = new CardDao(this);
        
        initViews();
    }
	
	private void initViews() {
//		Intent intent = getIntent();
//		String card = intent.getStringExtra(Constants.MEMBER_CARD);
//		EditText cardET = (EditText)findViewById(R.id.member_card_et);
//		cardET.setText(card);
		
		
		ImageView activeBtn = (ImageView) findViewById(R.id.member_save_btn);
		activeBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO
				boolean isOK = true;
				
				EditText card_id = 	(EditText)findViewById(R.id.member_card_id_et);
				EditText member_id = 	(EditText)findViewById(R.id.member_id_number_et);
				EditText member_name = 	(EditText)findViewById(R.id.member_name_et);
				EditText member_phone = 	(EditText)findViewById(R.id.member_phone_et);
				EditText member_address = 	(EditText)findViewById(R.id.member_address_et);
				if( member_phone.getText().toString().length() == 0 )
				{
					member_phone.setError( "电话号码不能为空" );
					isOK = false;
				}
				if( member_id.getText().toString().length() == 0 )
				{
					member_id.setError( "身份证号不能为空" );
					isOK = false;
				}
				if( member_name.getText().toString().length() == 0 )
				{
					member_name.setError( "姓名不能为空" );
					isOK = false;
				}
				if(!isOK) return;
				Member memberTemp = memberDao.findMemberByShfzId(member_id.getText().toString());
				if(memberTemp != null) {
					ViewUtil.makeToast(InfoInputActivity.this, "身份证信息已经存在,不能重复录入!");
					return;
				}
				
				Member member = new Member();
				member.setCardId(card_id.getText().toString());
				member.setName(member_name.getText().toString());
				member.setAddress(member_address.getText().toString());
				member.setPhone(member_phone.getText().toString());
				member.setCardNumber(card_id.getText().toString());
				member.setShfzID(member_id.getText().toString());
				Card card = new Card();
				card.setCardNumber(card_id.getText().toString());
				String id = cardDao.save(card);
				member.setCardId(id);
				String memberid = memberDao.save(member);
				Toast.makeText(getApplicationContext(), "会员数据添加成功", Toast.LENGTH_SHORT).show();
				Log.i(LOG_TAG, " add member id="+memberid);
				finish();
				
				
				
			}
		});
		
		ImageView cancelBtn = (ImageView) findViewById(R.id.member_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
}
